#ifndef __RC2412_BB_DEF_H__
#define __RC2412_BB_DEF_H__

#include <stdint.h>
#include "sdk_core/rc24xx.h"

#define BBE_REGB                           BBE
#define BBE_REGB_BASE                      BBE_BASE

#define BBE_SRAM_SIZE                      (0xA000) // 40KB

#define BBE_ABUF_BASE                      ((uint32_t)0x30000000)
#define BBE_ABUF0_BASE                     ((uint32_t)0x30000000)
#define BBE_ABUF1_BASE                     ((uint32_t)0x30004000)
#define BBE_ABUF2_BASE                     ((uint32_t)0x30008000)
#define BBE_ABUF0_SIZE                     (16*1024)
#define BBE_ABUF1_SIZE                     (16*1024)
#define BBE_ABUF2_SIZE                     ( 8*1024)

#define BBE_ABUF0_CMPLX_BASE               ((uint32_t)0x30020000)
#define BBE_ABUF1_CMPLX_BASE               ((uint32_t)0x30028000)
#define BBE_ABUF2_CMPLX_BASE               ((uint32_t)0x30030000)

#define BBE_ABUF0_OFFSET                   ((uint32_t)0x0)
#define BBE_ABUF1_OFFSET                   ((uint32_t)0x4000)
#define BBE_ABUF2_OFFSET                   ((uint32_t)0x8000)

#define BBE_ABS_OFFSET                     (0x10000)
#define BBE_ABUF0_ABS_BASE                 (BBE_ABUF0_BASE+BBE_ABS_OFFSET)
#define BBE_ABUF1_ABS_BASE                 (BBE_ABUF1_BASE+BBE_ABS_OFFSET)
#define BBE_ABUF2_ABS_BASE                 (BBE_ABUF2_BASE+BBE_ABS_OFFSET)

typedef struct{
	uint16_t interIdx;
	uint16_t intraIdx;
}BBE_CFARRST_T;

/**********************************BIT Pos**********************************/
#define BB_BIT31_POS                           (31)
#define BB_BIT30_POS                           (30)
#define BB_BIT29_POS                           (29)
#define BB_BIT28_POS                           (28)
#define BB_BIT27_POS                           (27)
#define BB_BIT26_POS                           (26)
#define BB_BIT25_POS                           (25)
#define BB_BIT24_POS                           (24)
#define BB_BIT23_POS                           (23)
#define BB_BIT22_POS                           (22)
#define BB_BIT21_POS                           (21)
#define BB_BIT20_POS                           (20)
#define BB_BIT19_POS                           (19)
#define BB_BIT18_POS                           (18)
#define BB_BIT17_POS                           (17)
#define BB_BIT16_POS                           (16)
#define BB_BIT15_POS                           (15)
#define BB_BIT14_POS                           (14)
#define BB_BIT13_POS                           (13)
#define BB_BIT12_POS                           (12)
#define BB_BIT11_POS                           (11)
#define BB_BIT10_POS                           (10)
#define BB_BIT9_POS                            (9)
#define BB_BIT8_POS                            (8)
#define BB_BIT7_POS                            (7)
#define BB_BIT6_POS                            (6)
#define BB_BIT5_POS                            (5)
#define BB_BIT4_POS                            (4)
#define BB_BIT3_POS                            (3)
#define BB_BIT2_POS                            (2)
#define BB_BIT1_POS                            (1)
#define BB_BIT0_POS                            (0)

/*********************************PREP*******************************************/
//cfg0
#define PREP_CFG0_SAMP_EDGE_N                 (0)
#define PREP_CFG0_SAMP_EDGE_P                 (1)

#define PREP_CFG0_ADC_CLK_MODE_P              (1)
#define PREP_CFG0_ADC_CLK_MODE_N              (0)
//cfg1
#define PREP_CFG1_ADC_DSIZE_8BIT              (0)
#define PREP_CFG1_ADC_DSIZE_16BIT             (1)

#define PREP_CFG1_CIC_SEC0                    (0)
#define PREP_CFG1_CIC_SEC1                    (1)
#define PREP_CFG1_CIC_SEC2                    (2)
#define PREP_CFG1_CIC_SEC3                    (3)
#define PREP_CFG1_CIC_SEC4                    (4)

#define PREP_CFG1_DOWN_FAC_1                  (0)
#define PREP_CFG1_DOWN_FAC_2                  (1)
#define PREP_CFG1_DOWN_FAC_3                  (2)
#define PREP_CFG1_DOWN_FAC_4                  (3)
#define PREP_CFG1_DOWN_FAC_5                  (4)
#define PREP_CFG1_DOWN_FAC_6                  (5)
#define PREP_CFG1_DOWN_FAC_7                  (6)
#define PREP_CFG1_DOWN_FAC_8                  (7)
#define PREP_CFG1_DOWN_FAC_9                  (8)
#define PREP_CFG1_DOWN_FAC_10                 (9)
#define PREP_CFG1_DOWN_FAC_11                 (10)
#define PREP_CFG1_DOWN_FAC_12                 (11)
#define PREP_CFG1_DOWN_FAC_13                 (12)
#define PREP_CFG1_DOWN_FAC_14                 (13)
#define PREP_CFG1_DOWN_FAC_15                 (14)
#define PREP_CFG1_DOWN_FAC_16                 (15)
//cfg2
#define PREP_CFG2_BASEMODE_ABUF0              (0)
#define PREP_CFG2_BASEMODE_ABUF1              (1)
#define PREP_CFG2_BASEMODE_ABUF2              (2)

#define PREP_CFG2_CIRC_EN                     (1)
#define PREP_CFG2_CIRC_DIS                    (0)

#define PREP_CFG2_INTER_MODEx1                 (0) //interInc=samPt*1
#define PREP_CFG2_INTER_MODEx2                 (1) //interInc=samPt*2
#define PREP_CFG2_INTER_MODEx4                 (2) //interInc=samPt*4


/*********************************P2*******************************************/
#define P2_FIX2PF_LSFB_0BIT                    (0)
#define P2_FIX2PF_LSFB_2BIT                    (1)
#define P2_FIX2PF_LSFB_4BIT                    (2)
#define P2_FIX2PF_LSFB_6BIT                    (3)

#define P2_MAX_CLR_EN                          (1)
#define P2_MAX_CLR_DIS                         (0)

#define P2_FFT_MODE_DIS                        (0) //memory
#define P2_FFT_MODE_EN                         (1) //FFT

#define P2_DMA_DSIZE_8BIT                      (0) 
#define P2_DMA_DSIZE_16BIT                     (1) 
#define P2_DMA_DSIZE_32BIT                     (2) // 8bit/16bit: fix2pf    32bit: original

#define P2_RSF_DIV1                            (0)
#define P2_RSF_DIV2                            (1) // /2
#define P2_RSF_DIV4                            (2) // /4
#define P2_RSF_DIV8                            (3) // /8
#define P2_RSF_DIV16                           (4) // /16
#define P2_RSF_DIV32                           (5) // /32
#define P2_RSF_DIV64                           (6) // /64
#define P2_RSF_DIV128                          (7) // /128
#define P2_RSF_DIV256                          (8) // /256


#define P2_SUBMODE_0                           (0) //cpx  +/*  (ACC+ADD+MUL+MAC)
#define P2_SUBMODE_1                           (1) //abs +  (ACC)
#define P2_SUBMODE_2                           (2) //cpx -  (ADD)

#define P2_MODE_ACC                            (0) //ACC
#define P2_MODE_ADD                            (1) //ADD
#define P2_MODE_MUL                            (2) //MUL
#define P2_MODE_MAC                            (3) //MAC
#define P2_MODE_DMA                            (4) //DMA
#define P2_MODE_NCO                            (5) //NCO

#define P2_NCO_MODE1                           (1) //COS
#define P2_NCO_MODE2                           (2) //SIN
#define P2_NCO_MODE3                           (3) //COS+SIN


#define BBE_P2_CFG0(intraCnt,fix2pfLsfB,max_clr,p2_fft_mode,dma_dsize,rsfBit,sub_mode,mode)    			 (BBE->P2_CFG0 =\
																																																		 (intraCnt   )<< BB_BIT20_POS |\
																																																		 (fix2pfLsfB )<< BB_BIT17_POS |\
																																																		 (max_clr    )<< BB_BIT16_POS |\
																																																		 (p2_fft_mode)<< BB_BIT14_POS |\
																																																		 (dma_dsize  )<< BB_BIT12_POS |\
																																																		 (rsfBit     )<< BB_BIT8_POS  |\
																																																		 (sub_mode   )<< BB_BIT4_POS  |\
																																																		 (mode       )<< BB_BIT0_POS  )

#define BBE_P2_CFG1(jumpCnt,interCnt)              (BBE->P2_CFG1 = (jumpCnt      )<< BB_BIT12_POS |(interCnt     )<< BB_BIT0_POS)
#define BBE_P2_CFG2(ncoMode,nco_fcw)               (BBE->P2_CFG2 = (ncoMode      )<< BB_BIT24_POS |(nco_fcw      )<< BB_BIT0_POS)																	
#define BBE_P2_CFG3(src0JumpInc,src0InterInc)      (BBE->P2_CFG3 = (src0JumpInc  )<< BB_BIT16_POS |(src0InterInc )<< BB_BIT0_POS)																			
#define BBE_P2_CFG4(src0IntraInc,src0BaseAddr)     (BBE->P2_CFG4 = (src0IntraInc )<< BB_BIT16_POS |(src0BaseAddr )<< BB_BIT0_POS)																			
#define BBE_P2_CFG5(src1JumpInc,src1InterInc)      (BBE->P2_CFG5 = (src1JumpInc  )<< BB_BIT16_POS |(src1InterInc )<< BB_BIT0_POS)	
#define BBE_P2_CFG6(src1IntraInc,src1BaseAddr)     (BBE->P2_CFG6 = (src1IntraInc )<< BB_BIT16_POS |(src1BaseAddr )<< BB_BIT0_POS)	
#define BBE_P2_CFG7(dstJumpInc,dstInterInc)        (BBE->P2_CFG7 = (dstJumpInc   )<< BB_BIT16_POS |(dstInterInc  )<< BB_BIT0_POS)	
#define BBE_P2_CFG8(dstIntraInc,dstBaseAddr)       (BBE->P2_CFG8 = (dstIntraInc  )<< BB_BIT16_POS |(dstBaseAddr  )<< BB_BIT0_POS)	

#define BBE_P2_STA0_MAXJUMPIDX                    ((BBE->P2_STA0 & 0x3FF00000)>>BB_BIT20_POS)
#define BBE_P2_STA0_MAXINTERIDX                   ((BBE->P2_STA0 & 0xFFC00)   >>BB_BIT10_POS)
#define BBE_P2_STA0_MAXINTRAIDX                   ( BBE->P2_STA0 & 0x3FF)
/*********************************CFAR*******************************************/
#define CFAR_PEAKDET_EN                           (1)
#define CFAR_PEAKDET_DIS                          (0)
																								  
#define CFAR_CFARMODE_GO                          (0)  //GO
#define CFAR_CFARMODE_SO                          (1)  //SO
#define CFAR_CFARMODE_CA                          (2)  //CA

#define CFAR_WKMODE_CFAR                          (0)  //CFAR
#define CFAR_WKMODE_FIX                           (1)  //fix thres
#define CFAR_WKMODE_SUM                           (2)  //thresSum

#define CFAR_THRESDIV1                       			(0)
#define CFAR_THRESDIV2											 			(1)
#define CFAR_THRESDIV4											 			(2)
#define CFAR_THRESDIV8											 			(3)
#define CFAR_THRESDIV16										   			(4)








#define CFAR_DIVFAC1                            	(0)
#define CFAR_DIVFAC2                        			(1)
#define CFAR_DIVFAC4                        			(2)
#define CFAR_DIVFAC8                        			(3)
#define CFAR_DIVFAC16                       			(4)
#define CFAR_DIVFAC32                       			(5)



#define BBE_CFAR_CFG0(pdEn,cfarMode,wkMode)			(BBE->CFAR_CFG0 = (pdEn)<< BB_BIT8_POS |(cfarMode  )<< BB_BIT4_POS|(wkMode)<< BB_BIT0_POS)
#define BBE_CFAR_CFG1(interCnt,intraCnt)			  (BBE->CFAR_CFG1 = (interCnt)<< BB_BIT12_POS |(intraCnt)<< BB_BIT0_POS)
#define BBE_CFAR_CFG2(wrapDirA,wrapDirB,thresDiv,divFac,mulFac,searchSize,guardSize)			(BBE->CFAR_CFG2 = (wrapDirA)  << BB_BIT25_POS|\
																																																										 (wrapDirB)  << BB_BIT24_POS|\
																																																										 (thresDiv)  << BB_BIT20_POS|\
																																																										 (divFac)    << BB_BIT16_POS|\
																																																										 (mulFac)    << BB_BIT8_POS|\
																																																										 (searchSize)<< BB_BIT4_POS|\
																																																										 (guardSize) << BB_BIT0_POS)
#define BBE_CFAR_CFG3(cutThres)									(BBE->CFAR_CFG3 = (cutThres)<< BB_BIT0_POS)
#define BBE_CFAR_CFG4(targetLim)								(BBE->CFAR_CFG4 = (targetLim)<< BB_BIT0_POS)
#define BBE_CFAR_CFG5(srcInterInc)							(BBE->CFAR_CFG5 = (srcInterInc)<< BB_BIT0_POS)
#define BBE_CFAR_CFG6(srcIntraInc,srcBaseAddr)	(BBE->CFAR_CFG6 = (srcIntraInc)<< BB_BIT16_POS |(srcBaseAddr)<< BB_BIT0_POS)
#define BBE_CFAR_CFG7(wrapAddrA,wrapAddrB)			(BBE->CFAR_CFG7 = (wrapAddrA)<< BB_BIT16_POS|(wrapAddrB)<< BB_BIT0_POS)
#define BBE_CFAR_CFG8(dstBaseAddr)							(BBE->CFAR_CFG8 = (dstBaseAddr)<< BB_BIT0_POS)
/*********************************FFT*******************************************/
#define FFT_PT16                               (0)
#define FFT_PT32                               (1)
#define FFT_PT64                               (2)
#define FFT_PT128                              (3)
#define FFT_PT256                              (4)

#define FFT_MODE_FORWARD             					 (0)
#define FFT_MODE_INVERSE             					 (1)

#define FFT_WIN_SIZE_14BIT           		 			 (0)
#define FFT_WIN_SIZE_32BIT           		 			 (1)
																							 		
#define FFT_WIN_EN                   		 			 (1)
#define FFT_WIN_DIS                  		 			 (0)

#define BBE_FFT_CFG0(inPt,fftPt,mode)              (BBE->FFT_CFG0 = (inPt)<< BB_BIT8_POS |(fftPt)<< BB_BIT4_POS|(mode)<<BB_BIT0_POS)
#define BBE_FFT_CFG1(useA,useB)             			 (BBE->FFT_CFG1 = (useA)<< BB_BIT8_POS |(useB)<< BB_BIT0_POS)
#define BBE_FFT_CFG2(winBaseAddr,winSize,winEn)    (BBE->FFT_CFG2 = ((uint32_t)(winBaseAddr)<<BB_BIT16_POS|(winSize)<< BB_BIT1_POS |(winEn)<< BB_BIT0_POS))

/*********************************BBE reg*******************************************/





#define BBE_SIZE8        (0x0)
#define BBE_SIZE16       (0x1)
#define BBE_SIZE32       (0x2)
#define BBE_SIZE64       (0x3)
/*********************************IMR*******************************************/
#define BBE_ISR_CFAR_END             (1<<20)
#define BBE_ISR_P2_JUMP_END          (1<<19)
#define BBE_ISR_P2_INTER_END         (1<<18)
#define BBE_ISR_P2_INTRA_END         (1<<17)
#define BBE_ISR_FFT_UNLOAD_END       (1<<16)
#define BBE_ISR_RAMP_NEDGE           (1<< 4)
#define BBE_ISR_RAMP_PEDGE           (1<< 3)
#define BBE_ISR_PREP_FRAME_END       (1<< 2)
#define BBE_ISR_PREP_RAMP_END        (1<< 1)

#define BBE_ISR_CLEAR(val)           (BBE->ISR = (uint64_t)(val))
/*********************************OP_GATE*******************************************/
#define BBE_GATE_CFAR                (1<<3)
#define BBE_GATE_P2   		           (1<<2)
#define BBE_GATE_FFT                 (1<<1)
#define BBE_GATE_PREP                (1<<0)
#define BBE_OPGATE_EN(val)           (BBE->OP_GATE &= ~(uint64_t)(val))
#define BBE_OPGATE_DIS(val)          (BBE->OP_GATE |= ((uint64_t)(val)))
/*********************************OP_TRIG*******************************************/
#define BBE_TRIG_CFAR                (1<<3)
#define BBE_TRIG_P2                  (1<<2)
#define BBE_OPTRIG(val)              (BBE->OP_TRIG |= (uint64_t)(val))
/*********************************OP_CLEAR*******************************************/
#define BBE_CLEAR_CFAR               (1<< 3)
#define BBE_CLEAR_P2                 (1<< 2)
#define BBE_CLEAR_FFT                (1<< 1)
#define BBE_CLEAR_PREP               (1<< 0)
#define BBE_OPCLEAR(val)             (BBE->OP_CLEAR |= (uint64_t)(val))


#define NUM_BYTE8                   (1)
#define NUM_BYTE16                  (2)
#define NUM_BYTE32                  (4)

#define COPY_MODE_32BIT             (2)

#define NUMLOG_2   			        (1)
#define NUMLOG_4   			        (2)
#define NUMLOG_8   			        (3)
#define NUMLOG_16   		        (4)
#define NUMLOG_32   		        (5)
#define NUMLOG_64   		        (6)
#define NUMLOG_128   		        (7)
#define NUMLOG_256   		        (8)
#define NUMLOG_512   		        (9)
#define NUMLOG_1024   	        (10)


#endif
